package me.zhengjie.tools.restimport

import me.zhengjie.logging.annotation.Log
import me.zhengjie.tools.service.EmailService
import me.zhengjie.tools.domain.EmailConfig
import me.zhengjie.tools.domain.vo.EmailVo
import io.swagger.v3.oas.annotations.media.Schema
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.*

/**
 * 发送邮件
 * @author 郑杰
 * @date 2018/09/28 6:55:53
 */
@RestController
@RequestMapping("api/email")
@Schema(description = "工具：邮件管理")
class EmailController(private val emailService: EmailService) {
    @GetMapping
    fun get(): ResponseEntity<*> {
        return ResponseEntity(emailService.find(), HttpStatus.OK)
    }

    @Log("配置邮件")
    @PutMapping
    @Schema(description = "配置邮件")
    fun emailConfig(@Validated @RequestBody emailConfig: EmailConfig): ResponseEntity<*> {
        emailService.update(emailConfig, emailService.find())
        return ResponseEntity<Any?>(HttpStatus.OK)
    }

    @Log("发送邮件")
    @PostMapping
    @Schema(description = "发送邮件")
    @Throws(Exception::class)
    fun send(@Validated @RequestBody emailVo: EmailVo?): ResponseEntity<*> {
        emailService.send(emailVo, emailService.find())
        return ResponseEntity<Any?>(HttpStatus.OK)
    }
}